import axios from "axios";
import {
  // Spin,
  Message
} from "iview";
import {
  getVerificationData
} from "@/libs/util";
class HttpRequest {
  constructor(baseUrl = baseURL) {
    this.baseUrl = baseUrl;
    this.queue = {};
  }
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        //
      }
    };
    return config;
  }
  distroy(url) {
    delete this.queue[url];
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }
  interceptors(instance, url) {
    // 请求拦截
    instance.interceptors.request.use(config => {
      var data = getVerificationData();
      config.headers.token = data.token;
      config.headers.timestamp = data.timestamp;
      config.headers.sign = data.sign;
      // 添加全局的loading...
      if (!Object.keys(this.queue).length) {
        // Spin.show() // 不建议开启，因为界面不友好
      }
      this.queue[url] = true;
      return config;
    }, error => {
      return Promise.reject(error);
    });
    // 响应拦截
    instance.interceptors.response.use(res => {
      this.distroy(url);
      if (res.data.code === "0" || res.data.code === "1") {
        Message.success(res.data.msg);
      } else {
        Message.error(res.data.msg);
      }
      const {
        data,
        status
      } = res;
      return {
        data,
        status
      };
    }, error => {
      this.distroy(url);
      return Promise.reject(error);
    });
  }
  request(options) {
    const instance = axios.create();
    options = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance, options.url);
    return instance(options);
  }
}
export default HttpRequest;
